/**
 * 
 */
package optimization.naturalOptimization.particleSwarmOptimization.neighborhood;

import optimization.naturalOptimization.particleSwarmOptimization.ParticleSwarmOptimization;
import optimization.naturalOptimization.population.Population;
import optimization.naturalOptimization.population.individual.pso.Particle;

/**
 * Provides a neighborhood for the particle swarm algorithm where every particle
 * can communicate with every other particle.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class GlobalNeighborhood extends PSONeighborhood {

	/**
	 * Creates a new Neighborhood for a given PSO.
	 * 
	 * @param parentPSO
	 */
	public GlobalNeighborhood(ParticleSwarmOptimization parentPSO) {
		super(parentPSO);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * optimization.naturalOptimization.particleSwarmOptimization.neighborhood
	 * .PSONeighborhood#setNeighborhoodbest()
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void setNeighborhoodbest() {
		Population swarm = getParentPSO().getSwarm();
		Particle best = (Particle) swarm.getIndividual(0);
		for (int i = 1; i < swarm.getNumberOfIndividuals(); i++) {
			Particle current = (Particle) swarm.getIndividual(i);
			if (getFitness().compare(current.getPersonalBestFitness(),
					best.getPersonalBestFitness()) == 1) {
				best = current;
			}
		}
		for (int i = 0; i < swarm.getNumberOfIndividuals(); i++) {
			((Particle) swarm.getIndividual(i)).setNeighborhoodBest(best
					.getPersonalBest());
			((Particle) swarm.getIndividual(i)).setNeighborhoodBestFitness(best
					.getPersonalBestFitness());
		}

	}

	@Override
	public String getIndentification() {
		return "Global";
	}

}
